package com.spring.schedule_system.utils;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.builder.ExcelReaderBuilder;

import java.io.InputStream;
import java.io.OutputStream;
import java.util.Map;

/**
 * 提供读取excell的功能，demo在ExcelWriteDemo  ExelReadDemo
 */
public class ExcelUtil {

    /**
     * 流式读取excel 将数据行读取为map
     * @param inputStream
     * @param sheetNo
     * @param eventListener
     */
    public static void readExcelToMap(InputStream inputStream, Integer sheetNo, ExcelAnalysisEventListener<Map<Integer, String>> eventListener){
        ExcelReaderBuilder excelReader = EasyExcel.read(inputStream, eventListener);
        excelReader.sheet(sheetNo).doRead();
    }
    public static void readExcelToMap(InputStream inputStream, String sheetName, ExcelAnalysisEventListener<Map<Integer, String>> eventListener){
        ExcelReaderBuilder excelReader = EasyExcel.read(inputStream, eventListener);
        excelReader.sheet(sheetName).doRead();
    }

    /**
     * 流式读取 将数据行转换为对象
     * @param inputStream
     * @param sheetName
     * @param headerClass
     * @param eventListener
     * @param <T>
     */
    public static <T> void readExcelToObject(InputStream inputStream, String sheetName,Class<T> headerClass , ExcelAnalysisEventListener<T> eventListener){
        ExcelReaderBuilder excelReader = EasyExcel.read(inputStream,headerClass, eventListener);
        excelReader.sheet(sheetName).doRead();
    }

    public static <T> void readExcelToObject(InputStream inputStream, Integer sheetNo,Class<T> headerClass , ExcelAnalysisEventListener<T> eventListener){
        ExcelReaderBuilder excelReader = EasyExcel.read(inputStream,headerClass, eventListener);
        excelReader.sheet(sheetNo).doRead();
    }

    /**
     * 获取流式写入对象
     * @param outputStream
     * @return
     */
    public static ExcelWriterBO writeExcelBO(OutputStream outputStream){
        ExcelWriterBO excelWriterBO = new ExcelWriterBO(outputStream);
        return excelWriterBO;
    }

}
